<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author XPF
-->
<template>
  <div>
    <BasicModal v-bind="$attrs" @register="registerModal" @ok="handleSubmit" title="解释">
      <BasicForm @register="registerForm" />
    </BasicModal>
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewSportsSportsWeightModal',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { SportsWeight, sportsWeightSave, sportsWeightListData } from '/@/api/sports/sportsWeight';
  const { showMessage } = useMessage();
  const emit = defineEmits(['success', 'register']);
  const { t } = useI18n('sports.sportsWeight');
  const record = ref<SportsWeight>({} as SportsWeight);
  const inputFormSchemas: FormSchema[] = [
    {
      component: 'InputTextArea',
      field: 'proposal',
      defaultValue: '',
      colProps: { lg: 24, md: 24 },
      componentProps: {
        rows: 4,
        maxlength: '75',
        showCount: true,
        placeholder: '请输入建议，最大字数限制为75字',
        onChange: (e: ChangeEvent) => {
          console.log('change', e.target.value);
        },
      },
    },
  ];
  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 120,
    schemas: inputFormSchemas,
    baseColProps: { lg: 12, md: 24 },
  });

  let dataId = '';
  const [registerModal, { closeModal, setModalProps }] = useModalInner(async (data) => {
    resetFields();
    dataId = data;
    setModalProps({ loading: true });
    setFieldsValue({ proposal: data.proposal });
    setModalProps({ loading: false });
  });
  async function handleSubmit() {
    try {
      let data = await validate();
      let res = await sportsWeightSave(dataId, data);
      showMessage(res.message);
      setTimeout(closeModal);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(t('您填写的信息有误，请根据提示修正。'));
      }
      console.log('error', error);
    }
  }
</script>
